package com.xiashitech.agent.rule.usermatch.inst;

import com.google.auto.service.AutoService;
import com.xiashitech.agent.config.StartConfig;
import com.xiashitech.agent.dto.UserRequest;
import com.xiashitech.agent.rule.usermatch.base.UserInfoMatchRule;
import com.xiashitech.interfaces.agent.dto.AgentConfigDTO;
import ognl.Ognl;

import java.util.Date;
import java.util.Map;

//@AutoService(UserInfoMatchRule.class)
public class XSAuthenticationUserMatch<REQUEST> implements UserInfoMatchRule<REQUEST> {
    @Override
    public UserRequest extractUserInfo(REQUEST request) {
        if (request == null) {
            return UserRequest.builder().build();
        }
        UserRequest userRequest = UserRequest.builder().requestDate(String.valueOf(new Date().getTime())).build();
        try {
            if(request.getClass().getName().equals("org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken")) {
                Object authentication = request;
                Object principal = authentication.getClass().getMethod("getPrincipal").invoke(authentication);
                if(principal != null && principal.getClass().getName().equals("org.springframework.security.oauth2.jwt.Jwt")) {
                    Map<String, Object> jwtClaims = (Map)principal.getClass().getMethod("getClaims").invoke(principal);
                    if(jwtClaims != null) {
                        String userIdPath = AgentConfigDTO.getConfig().getUseridpath().get(StartConfig.getParamValueFromKey(StartConfig.systemId));
                        Object value = null;
                        try {
                            value = Ognl.getValue(userIdPath, jwtClaims);
                        } catch (Exception ee) {
                        }
                        if(value != null) {
                            userRequest.setUserid(value.toString());
                        }
                    }
                }
            } else if(request.getClass().getName().equals("org.springframework.security.authentication.UsernamePasswordAuthenticationToken")) {
                Object authentication = request;
                Object principal = authentication.getClass().getMethod("getPrincipal").invoke(authentication);
                if(principal != null) {
                    //获取用户id
                    String userIdPath = AgentConfigDTO.getConfig().getUseridpath().get(StartConfig.getParamValueFromKey(StartConfig.systemId));
                    Object value = null;
                    try {
                        value = Ognl.getValue(userIdPath, principal);
                    } catch (Exception ee) {

                    }
                    if(value != null) {
                        userRequest.setUserid(value.toString());
                    }
                }
            }
        } catch (Throwable e) {
            StartConfig.throwableStackPrinter(e);
        }
        return userRequest;
    }

    @Override
    public Integer getOrder() {
        return 5;
    }
}
